/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pnikosis.materialishprogress.sample.slice;

import com.pnikosis.materialishprogress.ProgressWheel;
import com.pnikosis.materialishprogress.sample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.util.Random;

/**
 * MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private ProgressWheel progressWheel;
    private ProgressWheel progressWheelInterpolated;
    private ProgressWheel progressWheelLinear;
    private ProgressWheel progressWheelInstant;
    private ProgressWheel customProgressWheel;
    private final String[] barColors = {"#FFE51468", "#1236EC", "#BA20E3", "#48F517", "#E9CC11"};
    private final String[] wheelColors = {"#FFFFFFFF", "#070707", "#D0D0CA"};
    private static final float zeroPercent = 0.0f;
    private static final float tenPercent = 0.1f;
    private static final float twentyfivePercent = 0.25f;
    private static final float fiftyPercent = 0.50f;
    private static final float seventyfivePercent = 0.75f;
    private static final float hunderedPercent = 1.0f;
    private Button wheelColorbtn;
    private Button barColorbtn;
    private Button startbtn;
    private Button resetbtn;
    private Text getDefaultValue;
    private Text getZeroText;
    private Text getTenText;
    private Text getTwentyfiveText;
    private Text getFiftyText;
    private Text getSeventyfiveText;
    private Text getHunderedText;
    private Text interpolatedValue;
    private Text linearValue;
    private Text instantValue;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initLayout();
        setListeners();
        defaultProgress();

        progressWheel.setInderminateVal(true);
        progressWheelInterpolated.setInderminateVal(true);
        progressWheelLinear.setInderminateVal(true);
        progressWheelInstant.setInderminateVal(true);
        customProgressWheel.setInderminateVal(true);

        instantProgressWheel(0.5f);
    }

    private void initLayout() {
        barColorbtn = (Button) findComponentById(ResourceTable.Id_barColor);
        wheelColorbtn = (Button) findComponentById(ResourceTable.Id_wheelColor);
        startbtn = (Button) findComponentById(ResourceTable.Id_start);
        resetbtn = (Button) findComponentById(ResourceTable.Id_stop);

        progressWheel = (ProgressWheel) findComponentById(ResourceTable.Id_progress_wheel);
        progressWheelInterpolated = (ProgressWheel) findComponentById(ResourceTable.Id_interpolatedwheel);
        progressWheelLinear = (ProgressWheel) findComponentById(ResourceTable.Id_linearWheel);
        progressWheelInstant = (ProgressWheel) findComponentById(ResourceTable.Id_instantWheel);
        customProgressWheel = (ProgressWheel) findComponentById(ResourceTable.Id_customWheel);

        getDefaultValue = (Text) findComponentById(ResourceTable.Id_defaultval);
        getZeroText = (Text) findComponentById(ResourceTable.Id_zero);
        getTenText = (Text) findComponentById(ResourceTable.Id_ten);
        getTwentyfiveText = (Text) findComponentById(ResourceTable.Id_twentyfive);
        getFiftyText = (Text) findComponentById(ResourceTable.Id_fifty);
        getSeventyfiveText = (Text) findComponentById(ResourceTable.Id_seventyfive);
        getHunderedText = (Text) findComponentById(ResourceTable.Id_hundered);

        interpolatedValue = (Text) findComponentById(ResourceTable.Id_interpolatedValue);
        linearValue = (Text) findComponentById(ResourceTable.Id_linearValue);
        instantValue = (Text) findComponentById(ResourceTable.Id_instantValue);

    }

    private void setListeners() {
        wheelColorbtn.setClickedListener(this);
        barColorbtn.setClickedListener(this);
        startbtn.setClickedListener(this);
        resetbtn.setClickedListener(this);

        getDefaultValue.setClickedListener(this);
        getZeroText.setClickedListener(this);
        getTenText.setClickedListener(this);
        getTwentyfiveText.setClickedListener(this);
        getFiftyText.setClickedListener(this);
        getSeventyfiveText.setClickedListener(this);
        getHunderedText.setClickedListener(this);
    }

    private void resetProgress() {
        int defaultBarColor = Color.getIntColor("#5588FF");
        int defaultWheelColor = Color.getIntColor("#FFFFFF");

        progressWheel.setBarColor(defaultBarColor);
        progressWheelInterpolated.setBarColor(defaultBarColor);
        progressWheelLinear.setBarColor(defaultBarColor);
        progressWheelInstant.setBarColor(defaultBarColor);
        customProgressWheel.setBarColor(defaultBarColor);

        progressWheel.setRimColor(defaultWheelColor);
        progressWheelInterpolated.setRimColor(defaultWheelColor);
        progressWheelLinear.setRimColor(defaultWheelColor);
        progressWheelInstant.setRimColor(defaultWheelColor);
        customProgressWheel.setRimColor(defaultWheelColor);

        progressWheelLinear.setLinearProgress(true);
    }

    private void instantProgressWheel(float value) {
        progressWheelInstant.setInderminateVal(true);
        progressWheelInstant.setInstantProgress(value);
        instantValue.setText(String.format("%.2f", value));
    }

    private void defaultProgress() {
        getDefaultValue.setBackground(fillBg());
        customProgressWheel.spin();
        progressWheelLinear.setProgress(zeroPercent);
        progressWheelInterpolated.setProgress(zeroPercent);
        progressWheelLinear.setCallback(new ProgressWheel.ProgressCallback() {
            @Override
            public void onProgressUpdate(float progress) {
                if (progress == zeroPercent) {
                    progressWheelLinear.setProgress(hunderedPercent);
                    return;
                }
                if (progress == hunderedPercent) {
                    progressWheelLinear.setProgress(zeroPercent);
                    return;
                }
                interpolatedValue.setText(String.format("%.2f", progress));
            }
        });
        progressWheelInterpolated.setCallback(new ProgressWheel.ProgressCallback() {
            @Override
            public void onProgressUpdate(float progress) {
                if (progress == zeroPercent) {
                    progressWheelInterpolated.setProgress(hunderedPercent);
                    return;
                }
                if (progress == hunderedPercent) {
                    progressWheelInterpolated.setProgress(zeroPercent);
                    return;
                }
                linearValue.setText(String.format("%.2f", progress));
            }
        });
    }

    private void setProgress(float progress) {
        progressWheelLinear.setCallback(new ProgressWheel.ProgressCallback() {
            @Override
            public void onProgressUpdate(float progress) {
                linearValue.setText(String.format("%.2f", progress));
            }
        });
        progressWheelInterpolated.setCallback(new ProgressWheel.ProgressCallback() {
            @Override
            public void onProgressUpdate(float progress) {
                interpolatedValue.setText(String.format("%.2f", progress));
            }
        });

        progressWheelLinear.setProgress(progress);
        progressWheelInterpolated.setProgress(progress);
        clearFields();
    }

    private void clearFields() {
        getDefaultValue.setBackground(null);
        getZeroText.setBackground(null);
        getTenText.setBackground(null);
        getTwentyfiveText.setBackground(null);
        getFiftyText.setBackground(null);
        getSeventyfiveText.setBackground(null);
        getHunderedText.setBackground(null);
    }

    private static Element fillBg() {
        ShapeElement element = new ShapeElement();
        element.setShape(ShapeElement.RECTANGLE);
        element.setRgbColor(RgbColor.fromArgbInt(Color.GRAY.getValue()));
        return element;
    }

    @Override
    public void onClick(Component component) {
        Random random = new Random();
        int color;
        switch (component.getId()) {
            case ResourceTable.Id_barColor:
                color = Color.getIntColor(barColors[random.nextInt(barColors.length)]);
                progressWheel.setBarColor(color);
                progressWheelLinear.setBarColor(color);
                progressWheelInterpolated.setBarColor(color);
                progressWheelInstant.setBarColor(color);
                customProgressWheel.setBarColor(color);
                break;
            case ResourceTable.Id_wheelColor:
                color = Color.getIntColor(wheelColors[random.nextInt(wheelColors.length)]);
                progressWheel.setRimColor(color);
                progressWheelLinear.setRimColor(color);
                progressWheelInterpolated.setRimColor(color);
                progressWheelInstant.setRimColor(color);
                customProgressWheel.setRimColor(color);
                break;
            case ResourceTable.Id_defaultval:
                defaultProgress();
                resetProgress();
                clearFields();
                getDefaultValue.setBackground(fillBg());
                break;
            case ResourceTable.Id_zero:
                setProgress(zeroPercent);
                getZeroText.setBackground(fillBg());
                break;
            case ResourceTable.Id_ten:
                setProgress(tenPercent);
                getTenText.setBackground(fillBg());
                break;
            case ResourceTable.Id_twentyfive:
                setProgress(twentyfivePercent);
                getTwentyfiveText.setBackground(fillBg());
                break;
            case ResourceTable.Id_fifty:
                setProgress(fiftyPercent);
                getFiftyText.setBackground(fillBg());
                break;
            case ResourceTable.Id_seventyfive:
                setProgress(seventyfivePercent);
                getSeventyfiveText.setBackground(fillBg());
                break;
            case ResourceTable.Id_hundered:
                setProgress(hunderedPercent);
                getHunderedText.setBackground(fillBg());
                break;
            case ResourceTable.Id_start:
                customProgressWheel.spin();
                break;
            case ResourceTable.Id_stop:
                customProgressWheel.stopSpinning();
                break;
        }
    }
}
